home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 287 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. From: austern@isolde.mti.sgi.com (Matt Austern)
  2. Message-ID: <AUSTERN.96Feb5113807@isolde.mti.sgi.com>
  3. X-Original-Date: 05 Feb 1996 19:38:07 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 06 Feb 96 02:41:44 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Cleaning auto_ptr copy semantics.
  9. Organization: SGI
  10. References: <01BAF15D.08B8EEE0@dino.int.com> <31153AC5.1776@suphys.physics.su.oz.au>
  11. Reply-To: austern@cardboard.mti.sgi.com
  12. In-Reply-To: John Max Skaller's message of 5 Feb 1996 00:09:26 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMRa/9uEDnX0m9pzZAQGNKgGAoqNK91Zn72guXIihZ+ruWWPUmNrzid31
  15.     Wx7cfE3U4FI46MMgW4/PLCAMaZhr36Y7
  16.     =QSR+
  17.  
  18. In article <31153AC5.1776@suphys.physics.su.oz.au> John Max Skaller
  19. <maxtal@suphys.physics.su.oz.au> writes:
  20.  
  21. > > It's good if you are going to create a STL
  22. > > container of auto_ptr's.  
  23. >     EGADS! NEVER DO THIS!!!!
  24. >     The following will CRASH FOR SURE:
  25. >     void f(vector<auto_ptr<X> >){}
  26. >     vector<auto_ptr<X> > vx; vx.push_front(new X);
  27. >     f(vx);
  28. >     vx[0]; // CRASH
  29.  
  30. This leads to an interesting question: given the (current or proposed)
  31. semantics of auto_ptr<X>, how many situations can you think of where
  32. it is actually useful and correct to transfer ownership from one
  33. auto_ptr to another?  It really doesn't seem to work quite right in
  34. most of the cases where you'd like to use it.  If, as I suspect,
  35. ownership transfer is only rarely valid, then maybe it shouldn't be
  36. given such innocent seeming syntax as assignment or copy construction.
  37. Or maybe it should be removed entirely.
  38.  
  39. The auto_ptr template is still useful even if ownership transfer is
  40. completely banned: you use it when you allocate something on the heap
  41. and you want to be sure that it's deallocated at the end of a block or
  42. in a destructor.  I'm beginning to think that the right decision is to
  43. restrict auto_ptr to thas one simple use, instead compromising its
  44. safety by trying to make it useful for other purposes as well.  Other
  45. pointer templates (such as reference-counted pointers) would be better
  46. for those other purposes.
  47.  
  48. It may not be possible to get those other pointer classes into the
  49. standard, but, as John points out, that's not too bad so long as they
  50. become a de facto extension.
  51. -- 
  52. Matt Austern
  53. SGI: MTI Compilers Group
  54. austern@isolde.mti.sgi.com
  55. ---
  56. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  57.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  58.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  59.